/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.helpdesk.controllers;

import com.helpdesk.beans.AccountsFacade;
import com.helpdesk.controllers.util.Cookier;
import com.helpdesk.controllers.util.JsfUtil;
import com.helpdesk.entities.Accounts;
import com.helpdesk.entities.HelpDeskEnums;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.view.facelets.FaceletContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JK
 */
@ManagedBean(name = "loginController")
@RequestScoped
public class LoginController implements Serializable {

    public static final String currentUserKey = "currentUser";
    private Accounts currentUser;
    private boolean remmemberMe;
    private Accounts editUser;
    @EJB
    private com.helpdesk.beans.AccountsFacade ejbFacade;

    public LoginController() {
    }

    public AccountsFacade getFacade() {
        return ejbFacade;
    }

    public boolean isRemmemberMe() {
        return remmemberMe;
    }

    public void setRemmemberMe(boolean remmemberMe) {
        this.remmemberMe = remmemberMe;
    }

    public Accounts getCurrentUser() {
        HttpSession session = JsfUtil.getSession(true);
        currentUser = (Accounts) session.getAttribute(currentUserKey);
        return currentUser;
    }

    public void setCurrentUser(Accounts currentUser) {
        this.currentUser = currentUser;
    }

    public Accounts getCurrentEditUser() {

        editUser = getCurrentUser();
        return editUser;
    }

    public void setCurrentEditUser(Accounts acc) {
        editUser = acc;
    }

    public String login(String username, String password) {
        Accounts acc = ejbFacade.findByUsername(username);
        if (acc != null && acc.getPassword().equals(password)) {
            if (remmemberMe) {
                Cookier.add("_ua", JsfUtil.getBase64(username + "|" + password));
            }
            //save current user
            HttpSession session = JsfUtil.getSession(true);
            session.setAttribute(currentUserKey, acc);
            //navigate 
            switch (acc.getRoles()) {
                case HelpDeskEnums.UserRoles.Employee:
                    return "employee";
                case HelpDeskEnums.UserRoles.TechMan:
                    return "techman";
                case HelpDeskEnums.UserRoles.Admin:
                    return "admin";
                default:
                    return "employee";
            }
        }
        JsfUtil.addErrorMessage(JsfUtil.getString("LoginFailed"));
        return "failed";
    }

    public void saveEditAccount() {
        if (editUser != null) {
            getFacade().edit(editUser);
            JsfUtil.addSuccessMessage("Edit user was success!");

        } else {
            JsfUtil.addSuccessMessage("Edit user wasn't success!");
        }
    }

    /**
     * Validate user by username and password
     *
     * @param username
     * @param password
     * @return Account
     */
    public final Accounts ValidateUser(String username, String password) {
        Accounts acc = ejbFacade.findByUsername(username);
        if ((acc != null && acc.getPassword().equals(password))) {
            return acc;
        }
        return null;
    }

    public String goToAdmin() {
        if (getCurrentUser().getRoles() == HelpDeskEnums.UserRoles.Admin) {
            return "go_to_admin";
        } else {
            return null;
        }
    }

    public String logout() {
        Cookier.remove("_ua");
        HttpSession session = JsfUtil.getSession(true);
        if (session != null) {

            session.invalidate();
            return "login";
        } else {
            JsfUtil.addErrorMessage("Logout isn't success!");
            return null;
        }
    }
}
